

package com.io.controller;


import com.io.dto.ApiResult;
import com.io.entity.SysUserEntity;
import com.io.exception.SelfException;
import com.io.service.SysUserRoleService;
import com.io.service.SysUserService;
import com.io.utils.PageUtils;
import com.io.utils.annotation.DataFilter;
import com.io.utils.annotation.SysLog;
import com.io.utils.shiro.ShiroUtils;
import com.io.utils.validator.Assert;
import com.io.utils.validator.ValidatorUtils;
import com.io.utils.validator.group.AddGroup;
import com.io.utils.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * The class/interface 系统用户
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
@Api(tags = "系统用户管理模块")
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends BaseController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @ApiOperation("所有用户列表")
    @PostMapping("/list")
    @RequiresPermissions("sys:user:list")
    @DataFilter(subDept = true, user = false)
    public ApiResult list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysUserService.queryPage(params);
        return ApiResult.ok(page.getList()).put("total", page.getTotalCount());
    }

    @ApiOperation("获取登录的用户信息")
    @GetMapping("/info")
    public ApiResult info() {
        return ApiResult.ok().put("user", getUser());
    }

    @ApiOperation("修改登录用户密码")
    @SysLog("修改密码")
    @PostMapping("/password")
    public ApiResult password(String password, String newPassword) {
        Assert.isBlank(newPassword, "新密码不为能空");
        //原密码
        password = ShiroUtils.sha256(password, getUser().getSalt());
        //新密码
        newPassword = ShiroUtils.sha256(newPassword, getUser().getSalt());
        //更新密码
        boolean flag = sysUserService.updatePassword(getUserId(), password, newPassword);
        if (!flag) {
            return ApiResult.error("原密码不正确");
        }
        return ApiResult.ok();
    }

    @ApiOperation("用户信息")
    @GetMapping("/info/{userId}")
    @RequiresPermissions("sys:user:info")
    public ApiResult info(@PathVariable("userId") Long userId) {
        SysUserEntity user = sysUserService.getById(userId);
        if (StringUtils.isEmpty(user)) {
            throw new SelfException("查询用户不存在！");
        }
        //获取用户所属的角色列表
        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);
        return ApiResult.ok(user);
    }

    @ApiOperation("保存用户")
    @SysLog("保存用户")
    @PostMapping("/save")
    @RequiresPermissions("sys:user:save")
    public ApiResult save(@RequestBody SysUserEntity user) {
        ValidatorUtils.validateEntity(user, AddGroup.class);
        sysUserService.saveUser(user);
        return ApiResult.ok();
    }

    @ApiOperation("修改用户")
    @SysLog("修改用户")
    @PostMapping("/update")
    @RequiresPermissions("sys:user:update")
    public ApiResult update(@RequestBody SysUserEntity user) {
        ValidatorUtils.validateEntity(user, UpdateGroup.class);
        sysUserService.update(user);
        return ApiResult.ok();
    }

    @ApiOperation("删除用户")
    @SysLog("删除用户")
    @PostMapping("/delete")
    @RequiresPermissions("sys:user:delete")
    public ApiResult delete(@RequestBody Long[] userIds) {
        if (ArrayUtils.contains(userIds, 1L)) {
            return ApiResult.error("超级管理员不能删除");
        }
        if (ArrayUtils.contains(userIds, getUserId())) {
            return ApiResult.error("当前用户不能删除");
        }
        sysUserService.removeByIds(Arrays.asList(userIds));
        return ApiResult.ok();
    }
}
